package com.tag.util.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CodeGeneratorUtil {
    public void generator() throws Exception {
        List<String> warnings = new ArrayList<String>();
        //重要配置：是否覆盖代码，生成类里面有自定义属性和方法时不要覆盖
        boolean overwrite = false;
        // 指定逆向工程配置文件
        InputStream is= CodeGeneratorUtil.class.getClassLoader().getResourceAsStream("generatorXml/mybatisGeneratorConfig_jiuaiTag.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(is);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        for(String msg:warnings){
            System.out.println(msg);
        }

    }

    public static void main(String[] args) throws Exception {
        try {
            CodeGeneratorUtil generatorSqlmap = new CodeGeneratorUtil();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
